package filehandling;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.FileReader;

import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import adventure.Adventure;

/**
 *
 * @author Michael Hanns
 *
 */
public class DumpChangeLog {

    public static String dumpToLogFile(Adventure a, String filename, String changesMade) {
        // Create folder
        File logFolder = new File(System.getProperty("user.dir") + "/log/");

        if (!logFolder.isDirectory()) {
            logFolder.mkdir();
        }

        File logDump = new File(System.getProperty("user.dir") + "/log/"
                + filename + ".txt");

        try {
            if (logDump.createNewFile()) {

                FileOutputStream out;
                PrintStream p;

                out = new FileOutputStream(logDump);

                p = new PrintStream(out);

                DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
                Date date = new Date();

                String outputText = "WITNA Editor Logfile for " + a.getName() + "\n---\n\n"
                        + "Changes made " + dateFormat.format(date) + " (version " + a.getVersion() + "):\n"
                        + changesMade;

                p.println(outputText);
                p.close();

                return "Changes made dumped to log successfully.";
            } else {
                String previousChanges = readLogFile(logDump);

                if (previousChanges.length() == 0) {
                    return "Error reading previous log file! Changelog dump aborted.";
                } else {

                    FileOutputStream out;
                    PrintStream p;

                    out = new FileOutputStream(logDump);
                    p = new PrintStream(out);

                    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
                    Date date = new Date();

                    String outputText = previousChanges
                            + "Changes made " + dateFormat.format(date) + " (version " + a.getVersion() + "):\n"
                            + changesMade;

                    p.println(outputText);
                    p.close();

                    return "Changes made dumped to log successfully.";
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "Error encountered while saving file. Log file might be read-only.";
        }
    }

    private static String readLogFile(File logDump) {
        try {
            BufferedReader bufferedReader = new BufferedReader(new FileReader(logDump));
            String line;
            String output = "";
            while ((line = bufferedReader.readLine()) != null) {
                output += line + "\n";
            }
            return output;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
}
